home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / emacs.lha / emacs-19.16 / lisp / time-stamp.el < prev    next >
Lisp/Scheme  |  1993-06-20  |  10KB  |  272 lines

  1. ;;; time-stamp.el --- Maintain last change time stamps in files edited by Emacs
  2. ;;; Copyright 1989, 1993 Free Software Foundation, Inc.
  3.  
  4. ;; Maintainer: Stephen Gildea <gildea@lcs.mit.edu>
  5. ;; Time-stamp: <93/06/20 17:36:04 gildea>
  6. ;; Keywords: tools
  7.  
  8. ;; This file is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 2, or (at your option)
  11. ;; any later version.
  12.  
  13. ;; This file is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. ;; GNU General Public License for more details.
  17.  
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  20. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. ;;; Commentary:
  23.  
  24. ;;; If you put a time stamp template anywhere in the first 8 lines of a file,
  25. ;;; it can be updated every time you save the file.  See the top of
  26. ;;; time-stamp.el for a sample.  The template looks like one of the following:
  27. ;;;     Time-stamp: <>
  28. ;;;     Time-stamp: ""
  29. ;;; The time stamp is written between the brackets or quotes, resulting in
  30. ;;;     Time-stamp: <93/06/18 10:26:51 gildea>
  31. ;;; Here is an example which puts the file name and time stamp in the binary:
  32. ;;; static char *time_stamp = "sdmain.c Time-stamp: <>";
  33.  
  34. ;;; To activate automatic time stamping, add this code to your .emacs file:
  35. ;;;
  36. ;;; (autoload 'time-stamp "time-stamp" "Update the time stamp in a buffer." t)
  37. ;;; (if (not (memq 'time-stamp write-file-hooks))
  38. ;;;     (setq write-file-hooks
  39. ;;;           (cons 'time-stamp write-file-hooks)))
  40.  
  41. ;;; Change Log:
  42.  
  43. ;;; Originally based on the 19 Dec 88 version of
  44. ;;;   date.el by John Sturdy <mcvax!harlqn.co.uk!jcgs@uunet.uu.net>
  45.  
  46. ;;; Code:
  47.  
  48. (defvar time-stamp-active t
  49.   "*Non-nil to enable time-stamping of files.  See the function time-stamp.")
  50.  
  51. (defvar time-stamp-format
  52.   '(time-stamp-yy/mm/dd time-stamp-hh:mm:ss user-login-name)
  53.   "*A list of functions to call to generate the time stamp string.
  54. Each element of the list is called as a function and the results are
  55. concatenated together separated by spaces.  Elements may also be strings,
  56. which are included verbatim.  Spaces are not inserted around literal strings.")
  57.  
  58. ;;; Do not change time-stamp-line-limit, time-stamp-start, or
  59. ;;; time-stamp-end in your .emacs or you will be incompatible
  60. ;;; with other people's files!  If you must change them,
  61. ;;; do so only in the local variables section of the file itself.
  62.  
  63. (defvar time-stamp-line-limit 8        ;Do not change!  See comment above.
  64.   "Number of lines at the beginning of a file that are searched.
  65. The patterns time-stamp-start and time-stamp-end must be found on one
  66. of the first time-stamp-line-limit lines of the file for the file to
  67. be time-stamped.")
  68.  
  69. (defvar time-stamp-start "Time-stamp: \\\\?[\"<]+"    ;Do not change!
  70.   "Regexp after which the time stamp is written by \\[time-stamp].
  71. See also the variables  time-stamp-end  and  time-stamp-line-limit.
  72.  
  73. Do not change time-stamp-line-limit, time-stamp-start, or
  74. time-stamp-end for yourself or you will be incompatible
  75. with other people's files!  If you must change them for some application,
  76. do so in the local variables section of the time-stamped file itself.")
  77.  
  78.  
  79. (defvar time-stamp-end "\\\\?[\">]"    ;Do not change!  See comment above.
  80.   "Regexp marking the text after the time stamp.
  81. \\[time-stamp] deletes the text between the first match of  time-stamp-start
  82. \(which see) and the following match of  time-stamp-end  on the same line,
  83. then writes the time stamp specified by  time-stamp-format  between them.")
  84.  
  85. (defun time-stamp ()
  86.   "Update the time stamp string in the buffer.
  87. Only does its thing if the variable  time-stamp-active  is non-nil.
  88. Typically used on  write-file-hooks  for automatic time-stamping.
  89. The format of the time stamp is determined by the variable
  90. time-stamp-format.  The first  time-stamp-line-limit  lines of the
  91. buffer (normally 8) are searched for the time stamp template,
  92. and if it is found, a new time stamp is written into it."
  93.   (interactive)
  94.   (if time-stamp-active
  95.        (let ((case-fold-search nil))
  96.      (if (and (stringp time-stamp-start)
  97.           (stringp time-stamp-end))
  98.          (save-excursion
  99.            (goto-char (point-min))
  100.            (if (re-search-forward time-stamp-start
  101.                       (save-excursion
  102.                     (forward-line time-stamp-line-limit)
  103.                     (point))
  104.                       t)
  105.            (let ((start (point)))
  106.              (if (re-search-forward time-stamp-end
  107.                         (save-excursion (end-of-line) (point))
  108.                         t)
  109.              (let ((end (match-beginning 0)))
  110.                (delete-region start end)
  111.                (goto-char start)
  112.                (insert (time-stamp-string))
  113.                (setq end (point))
  114.                ;; remove any tabs used to format the time stamp
  115.                (goto-char start)
  116.                (if (search-forward "\t" end t)
  117.                    (untabify start end)))))))
  118.        ;; don't signal an error in a write-file-hook
  119.        (message "time-stamp-start or time-stamp-end is not a string"))))
  120.   ;; be sure to return nil so can be used on write-file-hooks
  121.   nil)
  122.  
  123. (defun time-stamp-string ()
  124.   "Generate the new string to be inserted by \\[time-stamp]."
  125.   (time-stamp-fconcat time-stamp-format " "))
  126.  
  127. (defun time-stamp-fconcat (list sep)
  128.   "Similar to (mapconcat 'funcall LIST SEP) but LIST can have literals.
  129. If an element of LIST is a symbol, it is funcalled to get the string to use;
  130. the separator SEP is used between two strings obtained by funcalling a
  131. symbol.  Otherwise the element itself is inserted; no separator is used
  132. around literals."
  133.   (let ((return-string "")
  134.     (insert-sep-p nil))
  135.     (while list
  136.       (cond ((symbolp (car list))
  137.          (if insert-sep-p
  138.          (setq return-string (concat return-string sep)))
  139.          (setq return-string (concat return-string (funcall (car list))))
  140.          (setq insert-sep-p t))
  141.         (t
  142.          (setq return-string (concat return-string (car list)))
  143.          (setq insert-sep-p nil)))
  144.       (setq list (cdr list)))
  145.     return-string))
  146.  
  147.  
  148. (defconst time-stamp-month-numbers
  149.   '(("Jan" . 1) ("Feb" . 2) ("Mar" . 3) ("Apr" . 4) ("May" . 5) ("Jun" . 6)
  150.     ("Jul" . 7) ("Aug" . 8) ("Sep" . 9) ("Oct" . 10) ("Nov" . 11) ("Dec" . 12))
  151.   "Assoc list of months and their number.")
  152.  
  153. (defconst time-stamp-month-full-names
  154.   ["(zero)" "January" "February" "March" "April" "May" "June"
  155.    "July" "August" "September" "October" "November" "December"])
  156.  
  157. (defvar time-stamp-mail-host nil
  158.   "Name of the host where the user receives mail.
  159. See the function time-stamp-mail-host-name.")
  160.  
  161. ;;; Some useful functions to use in time-stamp-format
  162.  
  163. ;;; Could generate most of a message-id with
  164. ;;; '(yymmdd "" hhmm "@" mail-host-name)
  165.  
  166. (defun time-stamp-mail-host-name ()
  167.   "Return the name of the host where the user receives mail.
  168. This is the value of time-stamp-mail-host if bound and a string,
  169. otherwise the value of the function system-name."
  170.   (or (and (boundp 'time-stamp-mail-host)
  171.        (stringp time-stamp-mail-host)
  172.        time-stamp-mail-host)
  173.       (system-name)))
  174.  
  175. (defun time-stamp-current-year ()
  176.   "Return the current year as a four-character string."
  177.   (substring (current-time-string) -4))
  178.  
  179. ;;; pretty form, suitable for a title page
  180.  
  181. (defun time-stamp-month-dd-yyyy ()
  182.   "Return the current date as a string in \"Month dd, yyyy\" form."
  183.   (let ((date (current-time-string)))
  184.     (format "%s %02d, %s"
  185.         (aref time-stamp-month-full-names
  186.           (cdr (assoc (substring date 4 7) time-stamp-month-numbers)))
  187.         (string-to-int (substring date 8 10))
  188.         (substring date -4))))
  189.  
  190. ;;; same as __DATE__ in ANSI C
  191.  
  192. (defun time-stamp-mon-dd-yyyy ()
  193.   "Return the current date as a string in \"Mon dd yyyy\" form.
  194. The first character of dd is Space if the value is less than 10."
  195.   (let ((date (current-time-string)))
  196.     (format "%s %2d %s"
  197.         (substring date 4 7)
  198.         (string-to-int (substring date 8 10))
  199.         (substring date -4))))
  200.  
  201. ;;; RFC 822 date
  202.  
  203. (defun time-stamp-dd-mon-yy ()
  204.   "Return the current date as a string in \"dd Mon yy\" form."
  205.   (let ((date (current-time-string)))
  206.     (format "%02d %s %s"
  207.         (string-to-int (substring date 8 10))
  208.         (substring date 4 7)
  209.         (substring date -2))))
  210.  
  211. ;;; RCS 3 date
  212.  
  213. (defun time-stamp-yy/mm/dd ()
  214.   "Return the current date as a string in \"yy/mm/dd\" form."
  215.   (let ((date (current-time-string)))
  216.     (format "%s/%02d/%02d"
  217.         (substring date -2)
  218.         (cdr (assoc (substring date 4 7) time-stamp-month-numbers))
  219.         (string-to-int (substring date 8 10)))))
  220.  
  221. ;;; RCS 5 date
  222.  
  223. (defun time-stamp-yyyy/mm/dd ()
  224.   "Return the current date as a string in \"yyyy/mm/dd\" form."
  225.   (let ((date (current-time-string)))
  226.     (format "%s/%02d/%02d"
  227.         (substring date -4)
  228.         (cdr (assoc (substring date 4 7) time-stamp-month-numbers))
  229.         (string-to-int (substring date 8 10)))))
  230.  
  231. (defun time-stamp-yymmdd ()
  232.   "Return the current date as a string in \"yymmdd\" form."
  233.   (let ((date (current-time-string)))
  234.     (format "%s%02d%02d"
  235.         (substring date -2)
  236.         (cdr (assoc (substring date 4 7) time-stamp-month-numbers))
  237.         (string-to-int (substring date 8 10)))))
  238.  
  239. (defun time-stamp-dd/mm/yy ()
  240.   "Return the current date as a string in \"dd/mm/yy\" form."
  241.   (let ((date (current-time-string)))
  242.     (format "%02d/%02d/%s"
  243.         (string-to-int (substring date 8 10))
  244.         (cdr (assoc (substring date 4 7) time-stamp-month-numbers))
  245.         (substring date -2))))
  246.  
  247. (defun time-stamp-mm/dd/yy ()
  248.   "Return the current date as a string in \"mm/dd/yy\" form."
  249.   (let ((date (current-time-string)))
  250.     (format "%02d/%02d/%s"
  251.         (cdr (assoc (substring date 4 7) time-stamp-month-numbers))
  252.         (string-to-int (substring date 8 10))
  253.         (substring date -2))))
  254.  
  255. (defun time-stamp-hh:mm:ss ()
  256.   "Return the current time as a string in \"hh:mm:ss\" form."
  257.   (substring (current-time-string) 11 19))
  258.  
  259. (defun time-stamp-hh:mm ()
  260.   "Return the current time as a string in \"hh:mm\" form."
  261.   (substring (current-time-string) 11 16))
  262.  
  263. (defun time-stamp-hhmm ()
  264.   "Return the current time as a string in \"hhmm\" form."
  265.   (let ((date (current-time-string)))
  266.     (concat (substring date 11 13)
  267.         (substring date 14 16))))
  268.  
  269. (provide 'time-stamp)
  270.  
  271. ;;; time-stamp.el ends here
  272.